package com.xiaren.线程测试.线程加入_join;

public class ThreadJoin {
	public static void main(String[] args) {
		B b = new B();
		new A(b);
		// new B(a);
		// new A();
	}
}

class A extends Thread {
	int k = 5;
	B b = null;

	public A(B b) {
		this.b = b;
		start();
	}

	public A() {
		start();
	}

	@Override
	public void run() {
		super.run();
		while (true) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println("a");
			if (--k == 0) {
				return;
			}
			try {
				b.join();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}

class B extends Thread {
	int k = 5;
	A a = null;

	public B() {
		start();
	}

	@Override
	public void run() {
		super.run();
		while (true) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println("b");
			if (--k == 0) {
				return;
			}
		}
	}

}
